//
//  FakeTextfield.swift
//  AESOLAR_IOS
//
//  Created by Mr.Wang on 2019/10/28.
//  Copyright © 2019 Mr.Wang. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa

class FakeTextfield: UIView {

    let placeHolderLabel = UILabel()
    let textField = UITextField()
    
    var placeHolder: String {
        set {
            placeHolderLabel.text = newValue
        }
        get {
            return placeHolderLabel.text ?? ""
        }
    }
    
    var text: String {
        set {
            textField.text = newValue
        }
        get {
            return textField.text ?? ""
        }
    }
    
    var optional: Bool {
        set {
            textField.placeholder = newValue ? "非必填".localized : ""
        }
        get {
            return false
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = .white
        
        placeHolderLabel.setContentHuggingPriority(.required, for: .horizontal)
        placeHolderLabel.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        placeHolderLabel.textColor = #colorLiteral(red: 0.7490196078, green: 0.7490196078, blue: 0.7490196078, alpha: 1)
        self.addSubview(placeHolderLabel)
        placeHolderLabel.snp.makeConstraints { (make) in
            make.centerY.equalToSuperview()
            make.left.equalTo(15)
        }
        
        textField.textAlignment = .right
        textField.font = UIFont.systemFont(ofSize: 14, weight: .medium)
        textField.textColor = #colorLiteral(red: 0.2, green: 0.2, blue: 0.2, alpha: 1)
        self.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.right.equalTo(-15)
            make.top.bottom.equalTo(0)
            make.left.equalTo(placeHolderLabel.snp.right).offset(10)
        }
        
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

extension Reactive where Base == FakeTextfield {
    
    var text: ControlProperty<String?> {
        return base.textField.rx.text
    }
    
}
